package com.sunsun_china.www.aq;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.sunsun_china.www.aq.base.AqPeriod;
import com.sunsun_china.www.aq.base.MyApplication;

/**
 * 修改日期：2015-11-20
 * 		1. 时段至少保留一个激活项
 * 		2. 关闭的时段等于任意一个开启的时段，就是关闭
 */
@SuppressLint("NewApi") public class PeriodActivity extends ActionBarActivity {

	MyApplication mApp;

	Button mButtonPeriodOn1;
	Button mButtonPeriodOn2;
	Button mButtonPeriodOn3;
	Button mButtonPeriodOff1;
	Button mButtonPeriodOff2;
	Button mButtonPeriodOff3;

	ImageView mImageViewPeriodOn1;
	ImageView mImageViewPeriodOn2;
	ImageView mImageViewPeriodOn3;
	ImageView mImageViewPeriodOff1;
	ImageView mImageViewPeriodOff2;
	ImageView mImageViewPeriodOff3;

	TextView mTextViewPeriodValueOn1;
	TextView mTextViewPeriodValueOn2;
	TextView mTextViewPeriodValueOn3;
	TextView mTextViewPeriodValueOff1;
	TextView mTextViewPeriodValueOff2;
	TextView mTextViewPeriodValueOff3;

	Switch mSwitchPeriod1;
	Switch mSwitchPeriod2;
	Switch mSwitchPeriod3;

	// 时段类型，1：照明灯 2：冲浪泵 3：杀菌灯
	int mPeriodType = 0;

	AqPeriod mPeriod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_period);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		mApp = (MyApplication) getApplication();
		if (mApp.mCurrentDevice == null) {
			this.finish();
			return;
		}
		mApp.mPeriodUi = this;

		Intent mainIntent = getIntent();
		mPeriodType = mainIntent.getIntExtra("periodType", 1);
		mPeriod = new AqPeriod();

		mImageViewPeriodOn1 = (ImageView) findViewById(R.id.period_imageView1);
		mImageViewPeriodOn2 = (ImageView) findViewById(R.id.period_imageView3);
		mImageViewPeriodOn3 = (ImageView) findViewById(R.id.period_imageView5);
		mImageViewPeriodOff1 = (ImageView) findViewById(R.id.period_imageView2);
		mImageViewPeriodOff2 = (ImageView) findViewById(R.id.period_imageView4);
		mImageViewPeriodOff3 = (ImageView) findViewById(R.id.period_imageView6);

		mTextViewPeriodValueOn1 = (TextView) findViewById(R.id.period_textView3);
		mTextViewPeriodValueOn2 = (TextView) findViewById(R.id.period_textView8);
		mTextViewPeriodValueOn3 = (TextView) findViewById(R.id.period_textView13);
		mTextViewPeriodValueOff1 = (TextView) findViewById(R.id.period_textView5);
		mTextViewPeriodValueOff2 = (TextView) findViewById(R.id.period_textView10);
		mTextViewPeriodValueOff3 = (TextView) findViewById(R.id.period_textView15);

		mButtonPeriodOn1 = (Button) findViewById(R.id.period_button1);
		mButtonPeriodOn2 = (Button) findViewById(R.id.period_button3);
		mButtonPeriodOn3 = (Button) findViewById(R.id.period_button5);
		mButtonPeriodOff1 = (Button) findViewById(R.id.period_button2);
		mButtonPeriodOff2 = (Button) findViewById(R.id.period_button4);
		mButtonPeriodOff3 = (Button) findViewById(R.id.period_button6);

		mSwitchPeriod1 = (Switch) findViewById(R.id.period_switch1);
		mSwitchPeriod2 = (Switch) findViewById(R.id.period_switch2);
		mSwitchPeriod3 = (Switch) findViewById(R.id.period_switch3);

		switch (mPeriodType) {
			case 1:
				// 照明灯
				AqPeriod.copy(mApp.mCurrentDevice.mLampPeriod, mPeriod);
				this.setTitle("照明灯时段设置");
				mImageViewPeriodOn1.setImageResource(R.drawable.ic_lamp_on);
				mImageViewPeriodOn2.setImageResource(R.drawable.ic_lamp_on);
				mImageViewPeriodOn3.setImageResource(R.drawable.ic_lamp_on);
				mImageViewPeriodOff1.setImageResource(R.drawable.ic_lamp_off);
				mImageViewPeriodOff2.setImageResource(R.drawable.ic_lamp_off);
				mImageViewPeriodOff3.setImageResource(R.drawable.ic_lamp_off);
				break;
			case 2:
				// 冲浪水泵
				AqPeriod.copy(mApp.mCurrentDevice.mPumpPeriod, mPeriod);
				this.setTitle("冲浪泵时段设置");
				mImageViewPeriodOn1.setImageResource(R.drawable.ic_pump_on);
				mImageViewPeriodOn2.setImageResource(R.drawable.ic_pump_on);
				mImageViewPeriodOn3.setImageResource(R.drawable.ic_pump_on);
				mImageViewPeriodOff1.setImageResource(R.drawable.ic_pump_off);
				mImageViewPeriodOff2.setImageResource(R.drawable.ic_pump_off);
				mImageViewPeriodOff3.setImageResource(R.drawable.ic_pump_off);
				break;
			case 3:
				// 杀菌灯
				AqPeriod.copy(mApp.mCurrentDevice.mUvLampPeriod, mPeriod);
				this.setTitle("杀菌灯时段设置");
				mImageViewPeriodOn1.setImageResource(R.drawable.ic_uvlamp_on);
				mImageViewPeriodOn2.setImageResource(R.drawable.ic_uvlamp_on);
				mImageViewPeriodOn3.setImageResource(R.drawable.ic_uvlamp_on);
				mImageViewPeriodOff1.setImageResource(R.drawable.ic_uvlamp_off);
				mImageViewPeriodOff2.setImageResource(R.drawable.ic_uvlamp_off);
				mImageViewPeriodOff3.setImageResource(R.drawable.ic_uvlamp_off);
				break;
			default:
				mPeriodType = 0;
				break;
		}

		showPeriod(mPeriod);
		mSwitchPeriod1.setChecked(mPeriod.isEnable(1));
		mSwitchPeriod2.setChecked(mPeriod.isEnable(2));
		mSwitchPeriod3.setChecked(mPeriod.isEnable(3));

		mSwitchPeriod1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!mSwitchPeriod2.isChecked() && !mSwitchPeriod3.isChecked()) {
					// 不允许关闭
					Toast.makeText(mApp.mContext, "请至少保留一个有效时段", Toast.LENGTH_SHORT).show();
					mSwitchPeriod1.setChecked(true);
				}
			}
		});

		mSwitchPeriod2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!mSwitchPeriod1.isChecked() && !mSwitchPeriod3.isChecked())
				{
					// 不允许关闭
					Toast.makeText(mApp.mContext, "请至少保留一个有效                                                                                                                                                            cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                                                       xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxccccccccccccccccccccc                                                                                                                                                                                                                                                                                                                                                                        时段", Toast.LENGTH_SHORT).show();
					mSwitchPeriod2.setChecked(true);
				}
			}
		});

		mSwitchPeriod3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (!mSwitchPeriod1.isChecked() && !mSwitchPeriod2.isChecked())
				{
					// 不允许关闭
					Toast.makeText(mApp.mContext, "请至少保留一个有效时段", Toast.LENGTH_SHORT).show();
					mSwitchPeriod3.setChecked(true);
				}
			}
		});

		mButtonPeriodOn1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						String debugStr = "hourOfDay="+hourOfDay + ",minute= " + minute;
						Log.d("TEMP", debugStr);
						mPeriod.period1.hourOn = hourOfDay;
						mPeriod.period1.minOn = minute / 10 ;
						showPeriod(mPeriod);
						Log.d("TEMP",mPeriod.toString());
					}
				}, mPeriod.period1.hourOn, mPeriod.period1.minOn * 10, true, "时段1开启时间");
				mPicker.show();
			}
		});

		mButtonPeriodOff1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						mPeriod.period1.hourOff = hourOfDay;
						mPeriod.period1.minOff = minute / 10;
						showPeriod(mPeriod);
					}
				}, mPeriod.period1.hourOff, mPeriod.period1.minOff * 10, true, "时段1关闭时间");
				mPicker.show();
			}
		});

		mButtonPeriodOn2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						mPeriod.period2.hourOn = hourOfDay;
						mPeriod.period2.minOn = minute / 10;
						showPeriod(mPeriod);
					}
				}, mPeriod.period2.hourOn, mPeriod.period2.minOn * 10, true, "时段2开启时间");
				mPicker.show();
			}
		});

		mButtonPeriodOff2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						mPeriod.period2.hourOff = hourOfDay;
						mPeriod.period2.minOff = minute / 10;
						showPeriod(mPeriod);
					}
				}, mPeriod.period2.hourOff, mPeriod.period2.minOff * 10, true, "时段2关闭时间");
				mPicker.show();
			}
		});

		mButtonPeriodOn3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						mPeriod.period3.hourOn = hourOfDay;
						mPeriod.period3.minOn = minute / 10;
						showPeriod(mPeriod);
					}
				}, mPeriod.period3.hourOn, mPeriod.period3.minOn * 10, true, "时段3开启时间");
				mPicker.show();
			}
		});

		mButtonPeriodOff3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Dialog mPicker = new CustomTimePickerDialog(PeriodActivity.this, new TimePickerDialog.OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						mPeriod.period3.hourOff = hourOfDay;
						mPeriod.period3.minOff = minute / 10;
						showPeriod(mPeriod);
					}
				}, mPeriod.period3.hourOff, mPeriod.period3.minOff * 10, true, "时段3关闭时间");
				mPicker.show();
			}
		});
	}

	private void showPeriodTextViewValue(TextView periodTextView, int hour, int min) {
		Log.d("TEMP", "设置分钟"+min);
		periodTextView.setText(String.valueOf(hour) + ":" + (String.format("%1$02d", min).equals("60")?"00":String.format("%1$02d", min)));
	}

	public void showPeriod(AqPeriod period) {
		showPeriodTextViewValue(mTextViewPeriodValueOn1, period.period1.hourOn, period.period1.minOn);
		showPeriodTextViewValue(mTextViewPeriodValueOn2, period.period2.hourOn, period.period2.minOn);
		showPeriodTextViewValue(mTextViewPeriodValueOn3, period.period3.hourOn, period.period3.minOn);
		showPeriodTextViewValue(mTextViewPeriodValueOff1, period.period1.hourOff, period.period1.minOff);
		showPeriodTextViewValue(mTextViewPeriodValueOff2, period.period2.hourOff, period.period2.minOff);
		showPeriodTextViewValue(mTextViewPeriodValueOff3, period.period3.hourOff, period.period3.minOff);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_period, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.period_action_settings1) {
			// 恢复出厂设置
			switch (mPeriodType) {
				case 1:
					// 照明灯
					mPeriod.period1.hourOn = 7;
					mPeriod.period1.minOn = 0;
					mPeriod.period1.hourOff = 11;
					mPeriod.period1.minOff = 0;
					mPeriod.period2.hourOn = 12;
					mPeriod.period2.minOn = 0;
					mPeriod.period2.hourOff = 17;
					mPeriod.period2.minOff = 0;
					mPeriod.period3.hourOn = 18;
					mPeriod.period3.minOn = 0;
					mPeriod.period3.hourOff = 21;
					mPeriod.period3.minOff = 0;
					break;
				case 2:
					// 冲浪水泵
				case 3:
					// 杀菌灯
					mPeriod.period1.hourOn = 6;
					mPeriod.period1.minOn = 0;
					mPeriod.period1.hourOff = 6;
					mPeriod.period1.minOff = 30;
					mPeriod.period2.hourOn = 11;
					mPeriod.period2.minOn = 0;
					mPeriod.period2.hourOff = 11;
					mPeriod.period2.minOff = 30;
					mPeriod.period3.hourOn = 17;
					mPeriod.period3.minOn = 0;
					mPeriod.period3.hourOff = 17;
					mPeriod.period3.minOff = 30;
					break;
				default:
					break;
			}
			mSwitchPeriod1.setChecked(true);
			mSwitchPeriod2.setChecked(true);
			mSwitchPeriod3.setChecked(true);
			showPeriod(mPeriod);
			return true;
		}
		if (id == android.R.id.home) {
			// 返回并保存配置
			savePeriodConfig();
			mApp.mPeriodUi = null;
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			savePeriodConfig();
			mApp.mPeriodUi = null;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void savePeriodConfig() {
		if (!mSwitchPeriod1.isChecked()) {
			if (mSwitchPeriod2.isChecked()) {
				mPeriod.period1.copyFrom(mPeriod.period2);
			}
			else if (mSwitchPeriod3.isChecked()) {
				mPeriod.period1.copyFrom(mPeriod.period3);
			}
		}
		if (!mSwitchPeriod2.isChecked()) {
			if (mSwitchPeriod1.isChecked()) {
				mPeriod.period2.copyFrom(mPeriod.period1);
			}
			else if (mSwitchPeriod3.isChecked()) {
				mPeriod.period2.copyFrom(mPeriod.period3);
			}
		}
		if (!mSwitchPeriod3.isChecked()) {
			if (mSwitchPeriod1.isChecked()) {
				mPeriod.period3.copyFrom(mPeriod.period1);
			}
			else if (mSwitchPeriod2.isChecked()) {
				mPeriod.period3.copyFrom(mPeriod.period2);
			}
		}
		
		Log.d("TEMP", "periodType= " + mPeriodType);
		
		switch (mPeriodType) {
			case 1:
				// 照明灯
				if (!mApp.mCurrentDevice.mLampPeriod.compare(mPeriod)) {
					mApp.mCurrentDevice.setPeriodLamp(mPeriod);
				}
				break;
			case 2:
				// 冲浪水泵
				if (!mApp.mCurrentDevice.mPumpPeriod.compare(mPeriod)) {
					switch (mApp.mCurrentDevice.mDeviceInfo.mDeviceType)
					{
						case AQ107:
							mApp.mCurrentDevice.setPeriodUvLampPump(mPeriod);
							break;
						case AQ805:
						case AQ209:
						case AQ208:
							mApp.mCurrentDevice.setPeriodPump(mPeriod);
							break;
						default:
							break;
					}
				}
				break;
			case 3:
				// 杀菌灯
				if (!mApp.mCurrentDevice.mUvLampPeriod.compare(mPeriod)) {
					switch (mApp.mCurrentDevice.mDeviceInfo.mDeviceType)
					{
						case AQ107:
							mApp.mCurrentDevice.setPeriodUvLampPump(mPeriod);
							break;
						case AQ805:
						case AQ209:
						case AQ208:
							mApp.mCurrentDevice.setPeriodUvLamp(mPeriod);
							break;
						default:
							break;
					}
				}
				break;
			default:
				break;
		}
	}
}
